/*
 *  Copyright 2008 Martin Fryč (martin@fryc.eu).
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package eu.fryc.martin.micromq.storage.impl;

import eu.fryc.martin.micromq.storage.DestinationManager;
import eu.fryc.martin.micromq.storage.MicroMQStorage;
import javax.jms.Destination;
import javax.jms.InvalidDestinationException;
import javax.jms.JMSException;

/**
 * Implementation of generic MicorMQ parts.
 * 
 * @author mfryc
 */
public abstract class MicroMQStorageAbstractImpl implements MicroMQStorage {

	/** Queue destination manager. */
	private DestinationManager queueDestinationManager;

	/**
	 * Get destination manager by destination type.
	 * 
	 * @param destination
	 *            destination
	 * 
	 * @return destination manager
	 * 
	 * @throws javax.jms.JMSException
	 *             if such destination manager is not supported
	 */
	protected DestinationManager getDestinationManager(Destination destination) throws JMSException {
		if (destination instanceof javax.jms.Queue) {
			return queueDestinationManager;
		} else {
			throw new JMSException("Destination of type + "
					+ (destination != null ? destination.getClass().getName() : "null") + " is not supported.");
		}
	}

	/**
	 * Check if connection id is valid.
	 * 
	 * @param connectionId
	 *            connection id
	 * 
	 * @throws javax.jms.JMSException
	 *             if connection id is invalid
	 */
	protected void checkConnectionId(String connectionId) throws JMSException {
		if (connectionId == null || connectionId.isEmpty()) {
			throw new JMSException("Cannot put message for null or empty connection.");
		}
	}

	@Override
	public void createDestination(Destination destination) throws JMSException {
		getDestinationManager(destination).create(destination);
	}

	@Override
	public void deleteDestination(Destination destination) throws JMSException, InvalidDestinationException {
		getDestinationManager(destination).delete(destination);
	}
}
